#include "StdAfx.h"
#include "Performer.h"

CPerformer::CPerformer(void)
{
}

void CPerformer::ExecuteCode(vector <treePtr> trees, DUCK_TYPE duckType)
{
	if ( trees.empty() )
	{
		return;
	}

	vector <treePtr>::iterator it;
	for ( it = trees.begin(); it != trees.end(); ++it )
	{
		treePtr treeNode = *it;
		if ( treeNode->type == WHILE )
		{
			ExecuteWhile(treeNode, duckType);
		}
		else
		{
			ExecuteCommand(treeNode->type, duckType);
		}
	}
	
}

void CPerformer::ExecuteCommand(TREE_TYPE type, DUCK_TYPE duckType)
{
	switch ( type )
	{
		case FLY:
			if ( duckType == RUBBER )
			{			
				cout << "Rubber duck can't fly" << endl;
			}
			else
			{
				cout << "Duck is flying." << endl;
			}
			break;
		case QUACK:
			cout << "Duck is quacking." << endl;
			break;
		case RIGHT:
			cout << "Duck is turning right." << endl;
			break;
	}
}

bool CPerformer::GetVariableValue()
{
	return rand() > 0.5 * (RAND_MAX + 1);
}

void CPerformer::ExecuteWhile(treePtr tree, DUCK_TYPE duckType)
{
	while ( GetVariableValue() )
	{
		ExecuteCode(tree->right, duckType);
	}
}

CPerformer::~CPerformer(void)
{
}
